package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.Aiface;
import com.woniuxy.dudugua.entity.User;
import com.woniuxy.dudugua.service.AIFaceService;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.aifaceutil.AIFace;
import com.woniuxy.dudugua.util.qnyutil.QiniuUtil;
import com.woniuxy.dudugua.util.view.StoreNavBar;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("ai")
public class AIFaceController {
    @Autowired
    private AIFaceService aiFaceService;
    @Autowired
    private AIFace aiFace;
    @Autowired
    private QiniuUtil qiniuUtil;

    @RequestMapping("getbyid")
    public Result getById(Integer id){
        Aiface aiface = aiFaceService.selectById(id);
        return new Result("200","success",aiface,null);
    }
    @PostMapping("insert")
    public Result insert(@RequestParam("image") MultipartFile image) throws Exception {
        String filename = "aiface/"+UUID.randomUUID().toString();
        //上传文件到七牛云
        qiniuUtil.upload(filename,image.getInputStream());
        String s = qiniuUtil.fullPath(filename);
        Aiface aiface = new Aiface();
        aiface.setAifaceImg(s);
        aiface.setJson(aiFace.Start(s));
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        aiface.setUserId(user.getId());
        aiface.setAddTime(new Date());
        int insert = aiFaceService.insert(aiface);
        return new Result("200","success",aiface.getId(),null);
    }
    @RequestMapping("getlist")
    public Result selectList(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        List<Aiface> list = aiFaceService.selectListById(user.getId());
        return new Result("200","success",null,list);
    }
    @RequestMapping("getuser")
    public  Result getUser(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        return new Result("200","success",user,null);
    }
    @RequestMapping("getproduct")
    public Result getProduct(String example){
        List<StoreNavBar> list = aiFaceService.selectAIProduct(example);
        return new Result("200","success",null,list);
    }

}
